package cn.gs.controller.lambdaTest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class JDKfunctionTest {

    public static void main(String[] args) {
        //Supplier 接口测试
        int [] arr ={21,33,442,22,32};
        int maxNum = supplierTest (arr) ;
        System.out.println(maxNum);
        //Consumer 接口测试
        //consumerTest( e-> System.out.println(e) );
        consumerTest( e -> System.out.println(e.toUpperCase()) , f -> System.out.println(f.toLowerCase())  );
        //Comparator 接口测试
        String [] strs = {"ssew","2ewsdfs","ccdd","52wer"};
        Comparator <String> com= comparatorTest();
        /*Arrays.sort(strs,com);
        System.out.println(Arrays.toString(strs));*/
        Arrays.sort(strs, (s,t) -> s.length() - t.length() );
        System.out.println(Arrays.toString(strs));
        //Predicate 接口测试
        andMethod("HelloWorld" , one -> one.contains("H") , two -> two.contains("W"));
        orMethod( "HelloWorld" , one -> one.contains("f") , two -> two.contains("z"));
        negateMethod("HelloWorld" , s -> s.contains("f"));
        //Function 接口测试
        funcMethod( s-> Integer.parseInt(s) ,  i -> i *= 10  );
        String array = "周七,28";
        int res =getAge(array , one -> one.split("," )[1], two -> Integer.parseInt(two) , three -> three -= 10 ) ;
        System.out.println("实际年龄： "+ res);
    }
    /**
     * Supplier 接口测试
     */
    public static Object getMax(Supplier sup){
        return sup.get();
    }

    public static int supplierTest(int [] arr){
        int maxNum= (int) getMax( () ->
        {
            int temp = arr[0];
            for( int i=1 ; i< arr.length; i++) {
                if(arr[i] > temp){
                    temp = arr[i];
                }
            }
            return temp;
        } );
        return maxNum;
    }
    /**
     * Consumer 接口测试
     */
    public static void consumerTest(Consumer <String> cons ){
        cons.accept("hello");
    }

    public static void consumerTest(Consumer <String> fir , Consumer <String> sec){
        fir.andThen(sec).accept("hello");
    }

    /**
     * Comparator 接口测试
     */
    public static Comparator <String> comparatorTest(){
        Comparator <String> com= new Comparator<String>(){
            @Override
            public int compare(String s, String t1) {
                return s.length() - t1.length();
            }
        };
        return com;
    }

    /**
     * Predicate 接口测试
     */
    public static boolean andMethod(String str,Predicate <String> one , Predicate <String> two){
        boolean isValid=  one.and(two).test(str);
        System.out.println("and 测试的结果： "+ isValid);
        return isValid;
    }
    public static boolean orMethod(String str,Predicate <String> one , Predicate <String> two){
        boolean isValid=  one.or(two).test(str);
        System.out.println("or 测试的结果： "+ isValid);
        return isValid;
    }
    public static boolean negateMethod(String str,Predicate <String> pred){
        boolean isValid = pred.negate().test(str);
        System.out.println("negate 测试的结果： "+ isValid);
        return isValid;
    }

    /**
     * Function 接口测试
     */
    public static Integer funcMethod (Function <String,Integer> func01 , Function<Integer,Integer> func02) {
       int num = func01.andThen(func02).apply("10");
        System.out.println("计算结果： "+ num);
       return num;
    }
    public static Integer getAge(String str , Function <String,String> func01 , Function<String,Integer> func02 ,
                                 Function<Integer,Integer> func03 ){
        return func01.andThen(func02).andThen(func03).apply(str);
    }

}
